﻿using System;
using System.Threading;
namespace MyAsyncThread
{
    class Program
    {
        private static int newTask(int ms)
        {
            Console.WriteLine("任务开始");
            Thread.Sleep(ms);
            Random random = new Random();
            int n = random.Next(10000);
            Console.WriteLine("任务完成");
            return n;
        }
        private delegate int NewTaskDelegate(int ms);
        static void Main(string[] args)
        {
            Thread.Sleep(15000);
            NewTaskDelegate task = newTask;
            IAsyncResult asyncResult = task.BeginInvoke(10000, null, null);

            // EndInvoke方法将被阻塞5秒
            int result = task.EndInvoke(asyncResult);
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}
